package l1j.server.server.command.executor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import l1j.server.L1DatabaseFactory;
import l1j.server.server.Account;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.serverpackets.S_SystemMessage;
import l1j.server.server.utils.SQLUtil;

public class L1CheckCharStatus implements L1CommandExecutor{
	private L1CheckCharStatus() {
	}

	public static L1CommandExecutor getInstance() {
		return new L1CheckCharStatus();
	}

	@Override
	public void execute(L1PcInstance pc, String cmdName, String arg) {
		try {
			Account account = Account.load(arg);
			if (account != null) {
					Account.OnlineStatus(account, false);
					loadchar(account, pc);
				} else {
					pc.sendPackets(new S_SystemMessage("此玩家狀態不能被清除，請提示玩家到問題回報區留言。"));
				}
		}
		catch (Exception e) {
			pc.sendPackets(new S_SystemMessage("請輸入 : " + cmdName + " 玩家名稱。"));
		}
	}
	
	private void loadchar(Account account, L1PcInstance pc) {
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			con = L1DatabaseFactory.getInstance().getConnection();
			pstm = con.prepareStatement("SELECT * FROM characters Where account_name=?");
			pstm.setString(1, account.getName());
			rs = pstm.executeQuery();
			String i = "";
			while (rs.next()){
				int j;
				pc.sendPackets(new S_SystemMessage("============"));
				pc.sendPackets(new S_SystemMessage("帳號( "+account.getName()+" )內角色狀態"));
				pc.sendPackets(new S_SystemMessage("角色："+rs.getString("char_name")));				
				j = rs.getInt("OnlineStatus");
				if(j == 1)
					i = "是";
				else
					i = "否";
				pc.sendPackets(new S_SystemMessage("人物在線："+i));
				j = rs.getInt("DailyCave");
				if(j == 1)
					i = "是";
				else
					i = "否";
				pc.sendPackets(new S_SystemMessage("每日副本完成："+i));
				j = rs.getInt("HellTime");
				if(j > 0)
					i = "是";
				else
					i = "否";
				pc.sendPackets(new S_SystemMessage("該角色是否在地獄："+i));
			}
			pc.sendPackets(new S_SystemMessage("~~~~~~~~~~~~"));
			pc.sendPackets(new S_SystemMessage("帳號( "+account.getName()+" )的各項狀態"));
			if(account.isBanned())
				i = "封鎖";
			else
				i = "正常";
			pc.sendPackets(new S_SystemMessage("帳號狀態："+i));
			if(account.isOnlined())
				i = "是";
			else
				i = "否";
			pc.sendPackets(new S_SystemMessage("帳號在線："+i));
			pc.sendPackets(new S_SystemMessage("~~~~~~~~~~~~"));
		}
		catch (SQLException e) {
		}
		finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
	}

}
